package com.niuwa.p2p.action.channel;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

import com.niuwa.p2p.core.mvc.entity.JsonResult;
import com.niuwa.p2p.core.utils.StringUtil;
import com.niuwa.p2p.entity.channel.Corporation;
import com.niuwa.p2p.entity.credit.CreditInvestigationInfo;
import com.niuwa.p2p.opr.utils.SessionUtil;
import com.niuwa.p2p.service.channel.IChannelInfoCheckService;
import com.niuwa.p2p.service.credit.PengYuanCreditService;
import com.niuwa.p2p.vo.channel.ChannelInfoCheckVo;

public class ChannelInfoCheckAction extends ChannelCommonAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6439383978511379242L;

	/** 日志 */
	private static Logger logger = Logger.getLogger(ChannelInfoCheckAction.class);

	@Autowired
	private PengYuanCreditService pengYuanCreditService;

	@Autowired
	private IChannelInfoCheckService channelInfoCheckService;

	/**
	 * 跳转到渠道信息核验
	 * 
	 * @return 渠道信息核验
	 * 
	 */
	@Action("gotoChannelInfoCheck")
	public String gotoChannelInfoCheck() {
		logger.info("gotoChannelInfoCheck action execute success");
		return "channelInfoCheck";
	}

	/**
	 * 进行黑名单校验
	 * 
	 * @return
	 */
	@Action("blackListCheck")
	public String blackListCheck() {
		logger.info("blackListCheck action execute success");
		// 获取公司代码
		String channelCode = this.getChannelCode();
		if (StringUtil.isEmpty(channelCode)) {
			logger.info("empty channelCode!");
			setResult(new JsonResult(false, "没有找到渠道代码"));
			return NONE;
		}
		logger.info("get channelCode : " + channelCode);
		// 使用corpCode查询数据库中的数据
		try {
			setResult(new JsonResult(true, channelInfoCheckService.selectBlackList(channelCode, SessionUtil
					.getSessionUser().getId())));
		} catch (Exception e) {
			e.printStackTrace();
			setResult(new JsonResult(false, "查询出错"));
		}
		return NONE;
	}

	/**
	 * 进行征信验证
	 * 
	 * @return
	 */
	@Action("creditCheck")
	public String creditCheck() {
		logger.info("creditCheck action execute success");
		// 渠道代码（公司代码）
		String channelCode = this.getChannelCode();
		if (StringUtil.isEmpty(channelCode)) {
			logger.info("empty channelCode!");
			setResult(new JsonResult(false, "没有找到渠道代码"));
			return NONE;
		}
		// 查询出公司名称并进行征信验证
		Corporation corporation = channelInfoCheckService.getCorporationInfo(channelCode);
		if (null == corporation) {
			logger.info("no channel!");
			setResult(new JsonResult(false, "不存在此渠道"));
			return NONE;
		}
		ChannelInfoCheckVo corp = channelInfoCheckService.getCorp(channelCode);
		ChannelInfoCheckVo legalMan = channelInfoCheckService.getCorpLegalMan(channelCode);
		ChannelInfoCheckVo generalManager = channelInfoCheckService.getCorpGeneralManager(channelCode);
		List<ChannelInfoCheckVo> personShareholderList = channelInfoCheckService.getPersonShareholder(channelCode);
		List<ChannelInfoCheckVo> corpShareholderList = channelInfoCheckService.getCorpShareholder(channelCode);

		List<ChannelInfoCheckVo> shareholderList = new ArrayList<ChannelInfoCheckVo>();
		shareholderList.addAll(personShareholderList);
		shareholderList.addAll(corpShareholderList);

		List<ChannelInfoCheckVo> aicList = new ArrayList<ChannelInfoCheckVo>();
		List<ChannelInfoCheckVo> courtList = new ArrayList<ChannelInfoCheckVo>();

		// 查询该企业的征信信息
		List<Object> credits = new ArrayList<Object>();
		credits.add(corp);
		credits.add(legalMan);
		credits.add(generalManager);
		credits.add(personShareholderList);
		credits.add(corpShareholderList);
		channelInfoCheckService.checkCredit(credits, courtList, aicList);

		List<Object> ret = new ArrayList<Object>();
		ret.add(corp);
		ret.add(legalMan);
		ret.add(generalManager);
		ret.add(shareholderList);
		ret.add(aicList);
		ret.add(courtList);
		setResult(new JsonResult(true, ret));
		return NONE;
	}

	/**
	 * 展示个人征信报告
	 * 
	 * @return
	 */
	@Action("showPersonalCreditReport")
	public String showPersonalCreditReport() {
		logger.info("showPersonalCreditReport action execute success");
		String name = getParam("name");
		String documentNo = getParam("documentNo");
		if (StringUtil.isEmpty(name)) {
			logger.info("empty person name!");
			setResult(new JsonResult());
			return NONE;
		}
		if (StringUtil.isEmpty(documentNo)) {
			logger.info("empty person id!");
			setResult(new JsonResult());
			return NONE;
		}
		// 处理中文参数
		try {
			name = URLDecoder.decode(name, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			logger.info("person name decode error!");
			setResult(new JsonResult());
			return NONE;
		}
		// 获取这个人的个人征信报告并传入前台
		CreditInvestigationInfo creditInvestigationInfo = null;
		try {
			creditInvestigationInfo = pengYuanCreditService.getPersonCheckResult(name, documentNo);
		} catch (Exception e) {
			e.printStackTrace();
			logger.info("pengyuan info error!");
			setResult(new JsonResult());
			return NONE;
		}

		if (null == creditInvestigationInfo) {
			logger.info("empty pengyuan info!");
			setResult(new JsonResult());
			return NONE;
		}

		setObjectReSult(creditInvestigationInfo);
		return "personalCreditReport";
	}

	/**
	 * 展示企业征信报告
	 * 
	 * @return
	 */
	@Action("showCorpCreditReport")
	public String showCorpCreditReport() {
		logger.info("showCorpCreditReport action execute success");
		// 获取参数
		// 公司名
		String corpName = getParam("corpName");
		// 工商注册号(暂无)
		String registerNo = "";
		// 组织代码(暂无)
		String orgCode = "";

		// 校验
		if (StringUtil.isEmpty(corpName)) {
			logger.info("empty corp name!");
			setResult(new JsonResult());
			return NONE;
		}

		// 处理中文参数
		try {
			corpName = URLDecoder.decode(corpName, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			logger.info("corp name decode error!");
			setResult(new JsonResult());
			return NONE;
		}
		// 获取这家企业的征信数据并传入前台
		CreditInvestigationInfo creditInvestigationInfo = null;
		try {
			creditInvestigationInfo = pengYuanCreditService.getCompanyByCheckResult(corpName, registerNo, orgCode);
		} catch (Exception e) {
			e.printStackTrace();
			logger.info("pengyuan info error!");
			setResult(new JsonResult());
			return NONE;
		}
		if (null == creditInvestigationInfo) {
			logger.info("empty pengyuan info!");
			setResult(new JsonResult());
			return NONE;
		}
		setObjectReSult(creditInvestigationInfo);
		return "corpCreditReport";
	}

	@Action("checkCorpCredit")
	public String checkCorpCredit() {
		logger.info("checkCorpCredit action execute success");
		// 获取参数
		// 公司名
		String corpName = getParam("corpName");
		// 工商注册号(暂无)
		// String registerNo = "";
		// 组织代码(暂无)
		// String orgCode = "";

		// 校验
		if (StringUtil.isEmpty(corpName)) {
			logger.info("empty corp name!");
			setResult(new JsonResult());
			return NONE;
		}

		// 处理中文参数
		try {
			corpName = URLDecoder.decode(corpName, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			logger.info("corp name decode error!");
			setResult(new JsonResult());
			return NONE;
		}

		// 获取这家企业的征信数据并传入前台
		/*
		 * TODO 暂时不查询 CreditInvestigationInfo creditInvestigationInfo = null;
		 * try { creditInvestigationInfo =
		 * pengYuanCreditService.getCompanyByCheckResult(corpName, registerNo,
		 * orgCode); } catch (Exception e) { e.printStackTrace();
		 * logger.info("pengyuan info error!"); setResult(new JsonResult());
		 * return NONE; } if (null == creditInvestigationInfo) {
		 * logger.info("empty pengyuan info!"); setResult(new JsonResult());
		 * return NONE; }
		 */
		// 保存一条查询记录
		channelInfoCheckService.saveCorpCheckLog(corpName, SessionUtil.getSessionUser().getId());
		setResult(new JsonResult());
		return NONE;
	}

	@Action("checkPersonCredit")
	public String checkPersonCredit() {
		logger.info("checkPersonCredit action execute success");
		// 获取参数
		// 人名
		String name = getParam("name");
		// 身份证号
		String certNo = getParam("certNo");

		// 校验
		if (StringUtil.isEmpty(name) || StringUtil.isEmpty(certNo)) {
			logger.info("empty person info!");
			setResult(new JsonResult());
			return NONE;
		}

		// 处理中文参数
		try {
			name = URLDecoder.decode(name, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			logger.info("person name decode error!");
			setResult(new JsonResult());
			return NONE;
		}

		// 获取这个人的征信数据并传入前台
		/*
		 * TODO 暂时不查询 CreditInvestigationInfo creditInvestigationInfo = null;
		 * try { creditInvestigationInfo =
		 * pengYuanCreditService.getPersonCheckResult(name, certNo); } catch
		 * (Exception e) { e.printStackTrace();
		 * logger.info("pengyuan info error!"); setResult(new JsonResult());
		 * return NONE; } if (null == creditInvestigationInfo) {
		 * logger.info("empty pengyuan info!"); setResult(new JsonResult());
		 * return NONE; }
		 */
		// 保存一条查询记录
		channelInfoCheckService.saveCertCheckLog(name, certNo, SessionUtil.getSessionUser().getId());
		setResult(new JsonResult());
		return NONE;
	}

	public void setPengYuanCreditService(PengYuanCreditService pengYuanCreditService) {
		this.pengYuanCreditService = pengYuanCreditService;
	}

	public PengYuanCreditService getPengYuanCreditService() {
		return pengYuanCreditService;
	}

	public void setChannelInfoCheckService(IChannelInfoCheckService channelInfoCheckService) {
		this.channelInfoCheckService = channelInfoCheckService;
	}

	public IChannelInfoCheckService getChannelInfoCheckService() {
		return channelInfoCheckService;
	}

}
